# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
import copy
import logging

from itemadapter import ItemAdapter
from pymysql import cursors
from scrapy.exporters import JsonItemExporter
from twisted.enterprise import adbapi



class NovelInfoPipeline:
    def process_item(self, item, spider):

        items = {}
        items["title"] = item["title"]
        items["profile"] = item["profile"]
        items["novel_id"] = item["novel_id"]
        items["author"] = item["author"]
        items["img"] = item["img"]
        items["download_href"] = item["download_href"]
        items["category"] = item["category"]
        logging.warning(items)
        # 对象拷贝，深拷贝  --- 这里是解决数据重复问题！！！
        asynItem = copy.deepcopy(items)
        query = self.db_pool.runInteraction(self.insert_into, asynItem)
        # 如果sql执行发送错误,自动回调addErrBack()函数
        query.addErrback(self.handle_error, items, spider)
        return item

    def close_spider(self, spider):

        print('爬虫结束！')

    def insert_into(self,cursor,item):
        # print(1)
        # print(item['novel_id'])
        # print(2)

        str1 = item['profile'].replace("'","’",20)
        str2 = str1.replace('"',"“",20)
        sql = '''INSERT INTO novel_info (novel_id,title,author,category,profile,img,download_href) VALUES ('{}','{}','{}','{}',"{}",'{}','{}')'''.format(
             item['novel_id'], item['title'],item['author'],item["category"],str2,item['img'],item["download_href"])
        # 执行sql语句
        cursor.execute(sql)

    def __init__(self,db_pool):
        self.db_pool = db_pool



    @classmethod
    def from_settings(cls, settings):
        """类方法，只加载一次，数据库初始化"""
        db_params = dict(
            host=settings['MYSQL_HOST'],
            user=settings['MYSQL_USER'],
            password=settings['MYSQL_PASSWORD'],
            port=settings['MYSQL_PORT'],
            database=settings['MYSQL_DBNAME'],
            charset=settings['MYSQL_CHARSET'],
            use_unicode=True,
            # 设置游标类型
            cursorclass=cursors.DictCursor
        )
        # 创建连接池
        db_pool = adbapi.ConnectionPool('pymysql', **db_params)
        # 返回一个pipeline对象
        return cls(db_pool)

    def open_spider(self, spider):

        print("爬虫开始...")

    def handle_error(self, failure, item, spider):
        # #输出错误信息
        print("failure", failure)